package com.jy.adscm.operation.purchase.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.operation.purchase.service.PurchasebomService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * bom清单表 服务实现类
 * </p>
 *
 * @author lzf
 * @since 2019-05-14
 */
@Service
public class PurchasebomServiceImpl extends ServiceImpl<PurchasebomMapper, Purchasebom> implements PurchasebomService {
    @Autowired
    PurchasebomMapper purchasebomMapper;


    /**
     * 导入物料清单数据接口
     * 暂时是把一级目录总数写死的，以后可以修改成动态的
     * @return
     */
    @Override
    public String dataimport() {
        Wrapper<Purchasebom> wrapper=new EntityWrapper<>();
        wrapper.eq("machineType",4);
        List<Purchasebom> purchaseboms = purchasebomMapper.selectList(wrapper);


        for (int i = 1; i < 28; i++) { //遍历一级目录
            for (Purchasebom purchasebom : purchaseboms) {

                if (purchasebom.getNo() != null) {
                    String[] split = purchasebom.getNo().split("\\.");
                    if (split.length > 0) {
                        if (Integer.valueOf(split[0])==i){
                            if (purchasebom.getParentId()==1){
                                purchasebom.setParentId1(Integer.valueOf(i));
                            }
                            if(purchasebom.getParentId()==2){
                                purchasebom.setParentId2(split[0]+"."+split[1]);
                            }
                            if(purchasebom.getParentId()==3){
                                purchasebom.setParentId3(split[0]+"."+split[1]+"."+split[2]);
                            }
                            if(purchasebom.getParentId()==4){
                                purchasebom.setParentId4(split[0]+"."+split[1]+"."+split[2]+"."+split[3]);
                            }
                            purchasebomMapper.updateById(purchasebom);
                        }
                    }
                }
            }
        }
        return "成功";
    }

    @Override
    public String disableData() {
        List<Purchasebom> purchaseboms = purchasebomMapper.disadleData();
        Purchasebom distinct=new Purchasebom();
        distinct.setParentId1(28);
        distinct.setName("重复物料");
        purchasebomMapper.insert(distinct);
        for (Purchasebom purchasebom : purchaseboms) {
            purchasebom.setParentId2("28.2");
            purchasebom.setParentId3(null);
            purchasebom.setParentId4(null);
            purchasebomMapper.updateById(purchasebom);
        }
        return "成功";
    }

    @Override
    public List<Purchasebom> queryByParentId(Integer pid, String pid2, String pid3, String pid4,String type) {
        List<Purchasebom> purchaseboms=null;
//        Wrapper<Purchasebom> wrapper=new EntityWrapper<>();
//        wrapper.eq("parentId1",pid).or("parentId2",pid2);
        purchaseboms = purchasebomMapper.queryByAll(pid,pid2,pid3,pid4,type);
        return purchaseboms;
    }

    @Override
    public List<Purchasebom> queryByFirst() {
        return null;
    }


    public List<Purchasebom> queryByPid(){
        Wrapper<Purchasebom> wrapper=new EntityWrapper<>();
        wrapper.last("where parentId1 is not null");
        List<Purchasebom> purchaseboms = purchasebomMapper.selectList(wrapper);
//        if (purchaseboms!=null&&purchaseboms.size()>0){
//            getAllChildren(purchaseboms);
//        }

        return purchaseboms;
    }

    private void getAllChildren(List<Purchasebom> purchaseboms) {
        Wrapper<Purchasebom> wrapper = new EntityWrapper<>();
        for (Purchasebom purchasebom : purchaseboms) {
            wrapper.eq("parentId2", purchasebom.getParentId1());
            List<Purchasebom> purchaseboms1 = purchasebomMapper.selectList(wrapper);
            if (purchaseboms1 != null && purchaseboms1.size() > 0) {
                getAllChildren(purchaseboms1);
            }
            purchasebom.setChildren(purchaseboms1);

        }
    }
   /* public List<Purchasebom> queryByParentId(String pid,String pid2,String pid3,String pid4){
        List<Purchasebom> purchaseboms=null;
        Wrapper<Purchasebom> wrapper=new EntityWrapper<>();
        wrapper.last("order by no");
        for (int i = 1; i < 29; i++) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(pid,null)){
                wrapper.eq("parentId1",i);
            }
            purchaseboms = purchasebomMapper.selectList(wrapper);
            for (Purchasebom purchasebom : purchaseboms) {
                if (AdscmBaseUtils.getInstance().isNotEmpty(pid2,null)){
                    wrapper.or().eq("parentId2",i);
                }
                List<Purchasebom> purchaseboms1 = purchasebomMapper.selectList(wrapper);
                purchasebom.setChildren(purchaseboms1);
            }

//            purchaseboms.
        }




//        if (AdscmBaseUtils.getInstance().isNotEmpty(pid3,null)){
//            wrapper.or().eq("parentId3",pid3);
//        }
//        if (AdscmBaseUtils.getInstance().isNotEmpty(pid,null)){
//            wrapper.or().eq("parentId4",pid4);
//        }


        return purchaseboms;
    }

    @Override
    public List<Purchasebom> queryByFirst() {
        return purchasebomMapper.queryByFirst();
    }*/
}
